// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsInvestmentConfigReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsInvestmentReq;
import com.liuxinlong.modules.assets.controller.request.QueryProductConfigReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsCalculationReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsInvestmentProjectReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsInvestmentReq;
import com.liuxinlong.modules.assets.dto.CalculateDataDto;
import com.liuxinlong.modules.assets.service.AssetsInvestmentProjectService;
import com.liuxinlong.modules.assets.service.AssetsInvestmentService;
import com.liuxinlong.modules.assets.service.InvestmentConfigService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.common.controller.response.CommonSelectResp;
import com.liuxinlong.modules.common.entity.CommonSelectDto;
import com.liuxinlong.modules.entity.AssetsInvestmentProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固资在线测算控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/assets/investment")
public class AssetsInvestmentController {

    @Autowired
    private AssetsInvestmentService assetsInvestmentService;

    @Autowired
    private InvestmentConfigService investmentConfigService;

    @Autowired
    private AssetsInvestmentProjectService assetsInvestmentProjectService;

    /**
     * 分页查询固资在线测算信息列表
     *
     * @param req 请求信息
     * @return 固资在线测算信息列表
     */
    @PostMapping("page")
    public BaseResp pageAssetsInvestmentList(@RequestBody QueryAssetsInvestmentReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("projectNo", req.getProjectNo());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("operateUser", req.getOperateUser());
        queryMap.put("operateTime", req.getOperateTime());
        List<Map<String, Object>> dataList = assetsInvestmentService.pageAssetsInvestmentInfo(queryMap);
        int totalNum = assetsInvestmentService.getAssetsInvestmentInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询固资在线测算信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 获取固资在线测算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("detail")
    @LogAnnotation(value = "获取固资在线测算信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp getAssetsInvestmentDetail(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = assetsInvestmentService.getAssetsInvestmentDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("获取固资在线测算信息成功");
        return resp;
    }

    /**
     * 新增固资在线测算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增固资在线测算信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsInvestmentInfo(@RequestBody UpdateAssetsInvestmentReq req) {
        CalculateDataDto calculateData = new CalculateDataDto();
        calculateData.setBasicData(req.getBasicData());
        calculateData.setNewProductData(req.getNewProductData());
        calculateData.setReplaceProductData(req.getReplaceProductData());
        calculateData.setInvestData(req.getInvestData());
        calculateData.setExpensesData(req.getExpensesData());
        calculateData.setLossRateData(req.getLossRateData());
        calculateData.setTurnoverData(req.getTurnoverData());
        assetsInvestmentService.addAssetsInvestmentInfo(calculateData);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资在线测算信息成功");
        return resp;
    }

    /**
     * 修改固资在线测算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改固资在线测算信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsInvestmentInfo(@RequestBody UpdateAssetsInvestmentReq req) {
        CalculateDataDto calculateData = new CalculateDataDto();
        calculateData.setId(req.getId());
        calculateData.setBasicData(req.getBasicData());
        calculateData.setNewProductData(req.getNewProductData());
        calculateData.setReplaceProductData(req.getReplaceProductData());
        calculateData.setInvestData(req.getInvestData());
        calculateData.setExpensesData(req.getExpensesData());
        calculateData.setLossRateData(req.getLossRateData());
        calculateData.setTurnoverData(req.getTurnoverData());
        String id = assetsInvestmentService.updateAssetsInvestmentInfo(calculateData);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("id",id);
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("修改固资项目信息成功");
        return resp;
    }

    /**
     * 删除固资在线测算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除固资在线测算信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsInvestmentInfo(@RequestBody BaseDeleteReq req) {
        assetsInvestmentService.deleteAssetsInvestmentInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资在线测算信息成功");
        return resp;
    }

    /**
     * 新增固资在线测算信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("calculation-add")
    @LogAnnotation(value = "新增固资在线测算信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsCalculationInfo(@RequestBody UpdateAssetsCalculationReq req) {
        CalculateDataDto calculateData = new CalculateDataDto();
        calculateData.setId(req.getId());
        calculateData.setBasicData(req.getBasicData());
        calculateData.setNewProductData(req.getNewProductData());
        calculateData.setReplaceProductData(req.getReplaceProductData());
        calculateData.setInvestData(req.getInvestData());
        calculateData.setExpensesData(req.getExpensesData());
        calculateData.setLossRateData(req.getLossRateData());
        calculateData.setTurnoverData(req.getTurnoverData());
        Map<String, Object> dataMap = assetsInvestmentService.addAssetsCalculationInfo(calculateData);
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("新增固资在线测算信息成功");
        return resp;
    }

    /**
     * 获取固资在线测算结果信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("calculation-detail")
    @LogAnnotation(value = "获取固资在线测算结果信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp getAssetsCalculationDetail(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = assetsInvestmentService.getAssetsCalculationDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("获取固资在线测算结果信息成功");
        return resp;
    }

    /**
     * 获取产品配置信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("product-config-get")
//    @LogAnnotation(value = "获取产品配置信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp getProductConfigInfo(@RequestBody QueryProductConfigReq req) {
        Map<String, Object> dataMap = assetsInvestmentService.getProductConfigInfo(req.getCategory(), req.getModel(), req.getChannel(), req.getBrand());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("获取产品配置信息成功");
        return resp;
    }

    /**
     * 分页查询固资在线测算配置信息列表
     *
     * @param req 请求信息
     * @return 固资在线测算配置信息列表
     */
    @PostMapping("config")
    public BaseResp getAssetsInvestmentConfig(@RequestBody QueryAssetsInvestmentConfigReq req) {
        int configType = req.getConfigType();
        if (configType < 5) {
            List<CommonSelectDto> resultList = investmentConfigService.getCommonConfig(configType);
            CommonSelectResp resp = new CommonSelectResp();
            resp.setResultList(resultList);
            resp.setRetMessage("获取固资在线测算通用配置信息成功");
            return resp;
        } else {
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("configType", configType);
            queryMap.put("configBrand", req.getConfigBrand());
            queryMap.put("configClass", req.getConfigClass());
            queryMap.put("configChannel", req.getConfigChannel());
            queryMap.put("configYear", req.getConfigYear());
            Map<String, Object> dataMap = new HashMap<>();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (configType == 6) {
                dataList = investmentConfigService.getLossConfigInfo(queryMap);
            } else if (configType == 7) {
                dataList = investmentConfigService.getTurnoverConfigInfo(queryMap);
            } else {
                dataMap = investmentConfigService.getConfigInfo(queryMap);
            }
            BaseResp resp = new BaseResp();
            resp.setRetMessage("获取固资在线测算配置信息成功");
            resp.setDataMap(dataMap);
            resp.setDataList(dataList);
            return resp;
        }
    }

    /**
     * 新增固资在线测算项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("project-trans")
    @LogAnnotation(value = "新增固资在线测算项目信息", business = LogBusinessConstants.ASSETS_INVESTMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsInvestmentProject(@RequestBody UpdateAssetsInvestmentProjectReq req) {
        AssetsInvestmentProject assetsInvestmentProject = new AssetsInvestmentProject();
        assetsInvestmentProject.setId(req.getId());
        assetsInvestmentProject.setInvestmentId(req.getInvestmentId());
        assetsInvestmentProject.setRemark(req.getProjectRemark());
        assetsInvestmentProject.setPlant(req.getPlant());
        assetsInvestmentProject.setGreenNo(req.getGreenNo());
        assetsInvestmentProject.setApproveTime(req.getApproveTime());
        assetsInvestmentProjectService.addAssetsInvestmentProject(assetsInvestmentProject);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资在线测算项目信息成功");
        return resp;
    }
}
